package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealMapper;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealPageVO;
import com.sky.vo.SetmealVO;
import com.sky.vo.setmealPageQueryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Transactional
    @Override
    public void add(SetmealDTO setmealDTO) {

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmeal.setStatus(StatusConstant.ENABLE);
//        setmeal.setCreateTime(LocalDateTime.now());
//        setmeal.setUpdateTime(LocalDateTime.now());
//        setmeal.setCreateUser(BaseContext.getCurrentId());
//        setmeal.setUpdateUser(BaseContext.getCurrentId());
        System.out.println("------------------------------------------");
        System.out.println(setmeal.toString());
        setmealMapper.insertSetmeal(setmeal);


        Long id = setmeal.getId();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(id);
        }
        setmealMapper.insertSetmealDishes(setmealDishes);
    }

    @Override
    public setmealPageQueryVO page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealPageVO> page = setmealMapper.page(setmealPageQueryDTO);
        long total = page.getTotal();
        List<SetmealPageVO> result = page.getResult();

        return new setmealPageQueryVO(total,result);
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        //更新setmeal
        setmealMapper.update(setmealDTO);

        //删除之前的setmeal-dishs
        Integer id = Integer.parseInt(setmealDTO.getId().toString());
        setmealMapper.deleteDish(id);

        //新增dish
        List<SetmealDish> list = setmealDTO.getSetmealDishes();
        setmealMapper.insertSetmealDishes(list);
    }

    @Override
    public void delete(String ids) {
        String[] splitmeal = ids.split(",");
        Integer[] deleteMealIds = new Integer[splitmeal.length];
        for (int i = 0; i < splitmeal.length; i++) {
            deleteMealIds[i] = Integer.parseInt(splitmeal[i]);
            setmealMapper.deleteDish(deleteMealIds[i]);
        }
        setmealMapper.delete(deleteMealIds);

    }

    @Override
    public SetmealVO getById(String id) {
        SetmealVO setmealVO = setmealMapper.getById(Integer.parseInt(id));
        List<SetmealDish> dishById = setmealMapper.getDishById(Integer.parseInt(id));
        setmealVO.setSetmealDishes(dishById);
        return setmealVO;
    }

    @Override
    public void updateStatus(String status,String id) {
        Integer statusInt = Integer.parseInt(status);
        setmealMapper.updateStatus(status,Integer.parseInt(id));

    }


}
